package com.fqcar.fqdf.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.entity.CarListReqBean;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.DialogUtil;
import com.fqdf.framework.android.util.CheckLogicUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lvsidong on 2017/10/21.
 */

public class SelectConditionActivity extends BaseRequestActivity{

    @BindView(R.id.select_condition_car_state)
    TextView select_condition_car_state;
    @BindView(R.id.select_condition_machine_state)
    TextView select_condition_machine_state;
    @BindView(R.id.select_condition_charge_state)
    TextView select_condition_charge_state;
    @BindView(R.id.select_condition_charge_num)
    EditText select_condition_charge_num;
    @BindView(R.id.cancel)
    Button cancel;
    @BindView(R.id.confirm)
    Button confirm;

    @BindView(R.id.select_condition_car_state_layout)
    LinearLayout select_condition_car_state_layout;
    @BindView(R.id.select_condition_machine_state_layout)
    LinearLayout select_condition_machine_state_layout;
    @BindView(R.id.select_condition_charge_state_layout)
    LinearLayout select_condition_charge_state_layout;

    private CarListReqBean carListReqBean;

    private MaterialDialog carStateDialog;
    private MaterialDialog machineDialog;
    private MaterialDialog chargeStateDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_condition);
        ButterKnife.bind(this);
        carListReqBean = new CarListReqBean();
        initData();
        initListener();
    }
    private void initData(){
        if(getIntent().getParcelableExtra(Constants.BUNDLE_DATA) != null){
            carListReqBean = getIntent().getParcelableExtra(Constants.BUNDLE_DATA);
            updateView();
        }
    }
    private void updateView(){
        if(!CheckLogicUtil.isEmpty(carListReqBean.getStatus())){
            select_condition_car_state.setText(carListReqBean.getStatus());
        }
        if(!CheckLogicUtil.isEmpty(carListReqBean.getOnlineStatus())){
            select_condition_machine_state.setText(carListReqBean.getOnlineStatus());
        }
        if(!CheckLogicUtil.isEmpty(carListReqBean.getChargeStatus())){
            select_condition_charge_state.setText(carListReqBean.getChargeStatus());
        }
        if(!CheckLogicUtil.isEmpty(carListReqBean.getPower())){
            select_condition_charge_num.setText(carListReqBean.getPower());
        }/*else {
            select_condition_charge_num.setText("100");
        }*/

        /*select_condition_charge_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length() == 0){
                    carListReqBean.setSocNumber("100");
                }
            }
        });*/
    }
    private void initListener(){
        select_condition_car_state_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(carStateDialog == null) {
                    carStateDialog = new MaterialDialog.Builder(SelectConditionActivity.this)
                            .items(R.array.select_condition_car_state_list)
                            .autoDismiss(true)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                    dialog.dismiss();
                                    carListReqBean.setStatus(String.valueOf(text));
                                    select_condition_car_state.setText(text);
                                }
                            })
                            .build();
                    if(!carStateDialog.isShowing()){
                        carStateDialog.show();
                    }
                }else {
                    carStateDialog.show();
                }
            }
        });
        select_condition_machine_state_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(machineDialog == null){
                    machineDialog = new MaterialDialog.Builder(SelectConditionActivity.this)
                            .items(R.array.select_condition_machine_state_list)
                            .autoDismiss(true)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                    dialog.dismiss();
                                    carListReqBean.setOnlineStatus(String.valueOf(text));
                                    select_condition_machine_state.setText(text);
                                }
                            }).build();
                    if(!machineDialog.isShowing()){
                        machineDialog.show();
                    }
                }else {
                    machineDialog.show();
                }
            }
        });
        select_condition_charge_state_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(chargeStateDialog == null){
                    chargeStateDialog = new MaterialDialog.Builder(SelectConditionActivity.this)
                            .items(R.array.select_condition_charge_state_list)
                            .autoDismiss(true)
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                    dialog.dismiss();
                                    carListReqBean.setChargeStatus(String.valueOf(text));
                                    select_condition_charge_state.setText(text);
                                }
                            }).build();
                    if(!chargeStateDialog.isShowing()){
                        chargeStateDialog.show();
                    }
                }else {
                    chargeStateDialog.show();
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if(CheckLogicUtil.isEmpty(select_condition_car_state.getText().toString())
                        && CheckLogicUtil.isEmpty(select_condition_machine_state.getText().toString())
                        && CheckLogicUtil.isEmpty(select_condition_charge_state.getText().toString())
                        && CheckLogicUtil.isEmpty(select_condition_charge_num.getText().toString())){
                    DialogUtil.ToastMessage(SelectConditionActivity.this,"条件不能为空");
                    return;
                }*/

                if(!CheckLogicUtil.isEmpty(select_condition_charge_num.getText().toString().trim())){
                    int num = Integer.valueOf(select_condition_charge_num.getText().toString().trim());
                    if(num > 100 || num < 0){
                        DialogUtil.ToastMessage(SelectConditionActivity.this,"请输入正确的电量");
                        return;
                    }else {
                        carListReqBean.setPower(select_condition_charge_num.getText().toString().trim());//电量
                    }
                }else {
                    carListReqBean.setPower("100");//电量
                }

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constants.Tag.SELECT_CONDITION, carListReqBean);
                intent.putExtras(bundle);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
